package com.weibo.common;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.loftysoft.loftyDBO.CommonDBO;
import com.weibo.common.CommonUtil;



public class DB extends Thread
{
	private static final Logger _log = Logger.getLogger(DB.class);
	
	private static CommonDBO dbOper = (CommonDBO) SpringUtil.getBean("DBBeans.xml","dbOper");
	private static boolean isConnect=false; 

	//数据库连接操作	
	/**
	 * 连接到数据库
	 */
	
	public static boolean connectToDB()
	{
		try{
			if (dbOper.executeConnect())
			{
				isConnect=true;
				return true;
			}else
			{
				isConnect=false;
				return false;
			}	
		}catch(Exception e){
			_log.error(e);
			return false;
		}	
			
	}
	/**
	 * 断开数据库连接
	 *
	 */
	public static void disconnect() 
	{
		try {
			dbOper.executeDisConnect();
		} catch (SQLException e) {			
			_log.error(e);
		}
	}

	/**
	 * 数据库查询操作
	 */
	public static List sqlExeQuery(String sql)
	{
		List list = null;
		try
		{
			list = CommonUtil.convertList(dbOper.doExecuteQuery(sql));
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			/*while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}
			list=sqlExeQuery(sql);*/
		}
		return list;
	}

	//数据库非查询操作
	/**
	 * 执行单条的sql语句。
	 */
	public static boolean sqlNonExeQuery(String sql)
	{
		boolean isSucess=false;
		try
		{
			dbOper.executeUpdate(sql);
			isSucess=true;
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			/*while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}
			isSucess=sqlNonExeQuery(sql);*/
		}
		return isSucess;
	}
	/**
	 * 执行批量的sql语句，建议使用
	 * @param sql
	 * @return
	 */
	public static boolean sqlNonExeQuery(String[] sql)
	{
		boolean isSucess=false;
		try
		{
			dbOper.executeUpdate(sql);
			isSucess=true;
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			/*while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}
			//isSucess=sqlNonExeQuery(sql);
*/		}
		return isSucess;
	}
	
	public static ResultSet plsqlExec_res(String sql)
	{
		ResultSet rs = null;
		try
		{
			rs = dbOper.doExecuteQuery(sql);
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			_log.error(e);
			isConnect=false;
			disconnect();
			/*while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}
			//rs=sqlExeQuery(sql);
*/		}
		return rs;
	}
	
	/**
	 * 执行plsql句，建议使用
	 * @param sql
	 * @return
	 * DB.plsqlExec("pkg_alarminfo.product_alarminfo", "123");
	 */
	public static boolean plsqlExec(String sql,Object... para)
	{
		boolean isSucess=false;		
		try
		{
			//dbOper.executeStoredProcedures(sql, para);
			dbOper.execute(CommonUtil.getSqlBy_par(sql, para));
			isSucess=true;
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			/*while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}
			//isSucess=plsqlExec(sql);
*/		}
		return isSucess;
	}
	
	
	/**
	 * 执行plsql句，建议使用
	 * @param sql
	 * @return result
	 * StringBuffer result=new StringBuffer();
	 * DB.plsqlExecOut("pkg_alarminfo.product_alarminfo",result, "123","","");
	 */
	protected static boolean plsqlExecOut(String sql,StringBuffer result,Object... para)
	{
		boolean isSucess=false;		
		try
		{
			dbOper.executeStoredProcedureOut(sql, result,para);
			isSucess=true;
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
			}			
		}
		return isSucess;
	}
	
	
	public static ResultSet plsqlExecOut_rs(String sql,Object... para)
	{
		ResultSet rs = null;	
		try
		{
			String sql_ = CommonUtil.getSqlBy_par(sql, para);
			rs = dbOper.doExecuteQuery(sql_);
		}
		catch (SQLException e)
		{
			_log.error(e);
			isConnect=false;
			disconnect();
			while(!isConnect)
			{
				disconnect();
				try {
					sleep(3000);
				} catch (InterruptedException e1) {
					_log.error(e);
				}
				connectToDB();
				e.printStackTrace();
			}			
		}
		return rs;
	}
	
	
	public static void main(String args[]){
//		System.out.println(DB.isConnect);
//		System.out.println(DB.connectToDB());
//		System.out.println(DB.isConnect);
		//DB.sqlExeQuery("select count(*) as cont from alarminfo",Count.class);
		//DB.sqlNonExeQuery("insert into alarmfilter values('"+ne_id+"','"+type_id+"')")
		 // = DB.sqlExeQuery("select * from weibo_host_info", Host.class);
//			StringBuffer result=new StringBuffer();
		  List rs = DB.sqlExeQuery("select * from weibo_host_info");
		try {
			System.out.println(rs.size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Count count = (Count)list.get(0);
		//System.out.println(count.getCont());
	
	}
	
	
}
